package com.terren.spider.db.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.terren.spider.db.basic.BasicServiceSupportImpl;
import com.terren.spider.db.service.EntryService;
import com.terren.spider.entity.core.Entry;
import com.terren.spider.entity.core.EntryFormat;
import com.terren.spider.entity.core.HelpUrl;
import com.terren.spider.entity.core.Regular;
import com.terren.spider.entity.core.RegularGroupDiv;
@Service("entryService")
public class EntryServiceImpl extends BasicServiceSupportImpl implements EntryService{
	private final String ENTRY_SQL_MAPPER = "entryMapper.";
	private final String REGULAR_SQL_MAPPER = "regularMapper.";
	private final String HELPURL_SQL_MAPPER = "helpUrlMapper.";
	private final String FORMAT_SQL_MAPPER = "entryFormatMapper.";
	private final String REGULARGROUPDIV_SQL_MAPPER = "regularGroupDivMapper.";

	@Override
	public List<Entry> getEntryList(Integer[] entryIds) throws Exception {
		List<Entry> entryList =  basicDao.findForList(ENTRY_SQL_MAPPER+"getEntryList", entryIds);
		if (null!= entryList) {
			for (int i = 0; i < entryList.size(); i++) {
				initSubEntry(entryList.get(i));
				initEntry(entryList.get(i));
			}
		}
		return entryList;
	}

	@Override
	public Entry getEntryById(Integer entryId) throws Exception {
		Entry obj =  basicDao.findForObject(ENTRY_SQL_MAPPER+"getEntryById", entryId);
		if (obj!=null) {
			initEntry(obj);
		}
		return obj;
	}

	
	public void initEntry(Entry entry) throws Exception{
		Integer entryId = entry.getEntryId();
		List<Regular> regularList=  basicDao.findForList(REGULAR_SQL_MAPPER+"getRegularByEntryId", entryId);
		List<HelpUrl> helpUrlList=  basicDao.findForList(HELPURL_SQL_MAPPER+"getHelpUrlList", entryId);
		List<EntryFormat> formatList = basicDao.findForList(FORMAT_SQL_MAPPER+"getEntryFormatByEntryId", entryId);
		List<RegularGroupDiv> divList = basicDao.findForList(REGULARGROUPDIV_SQL_MAPPER+"getRegularGroupDivByEntryId", entryId);
		entry.setFormats(formatList);
		entry.setRegulars(regularList);
		entry.setHelpUrls(helpUrlList);
		entry.setDivList(divList);
	}
	
	public void initSubEntry(Entry entry)throws Exception {
		List<Entry> subEntryList=  basicDao.findForList(ENTRY_SQL_MAPPER+"getSubEntryList", entry.getEntryId());
		if(CollectionUtils.isNotEmpty(subEntryList)) {
			for (Entry sub : subEntryList) {
				initEntry(sub);
			}
			entry.setSubEntry(subEntryList);
		}
	}
	
	@Override
	public List<Entry> getEntryBySourceId(Integer sourceId) throws Exception {
		List<Entry> entryList =  basicDao.findForList(ENTRY_SQL_MAPPER+"getEntryBySourceId",sourceId);
		if (null!= entryList) {
			for (int i = 0; i < entryList.size(); i++) {
				initSubEntry(entryList.get(i));
				initEntry(entryList.get(i));
			}
		}
		return entryList;
	}

	@Override
	public List<Entry> getEntryByGroupId(Integer groupId) throws Exception {
		List<Entry> entryList =  basicDao.findForList(ENTRY_SQL_MAPPER+"getEntryByGroupId", groupId);
		if (null!= entryList) {
			for (int i = 0; i < entryList.size(); i++) {
				initSubEntry(entryList.get(i));
				initEntry(entryList.get(i));
			}
		}
		return entryList;
	}

}
